package com.atguigu.eduservice.controller;


import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapvdeovo.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-13
 */
@RestController
@RequestMapping("eduservice/chapter")
@Api(description="章节页面")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //根据课程id查询课程里面的章节和小节
    @GetMapping("getChapterVideoByCourse/{courseId}")
    @ApiOperation(value = "根据课程id查询课程里面的章节和小节")
    public R getChapterVideoByCourse(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getCourseChapterVideo(courseId);
        return R.ok().data("chaperVideo",list);
    }

    //1 添加章节接口
    @PostMapping("addChapter")
    @ApiOperation(value = "添加章节接口")
    public R addChapter(@RequestBody EduChapter chapter) {
        //TODO 完善判断课程里面章节不能重复添加
        chapterService.save(chapter);
        return R.ok();
    }

    //2 根据id查询
    @GetMapping("getChapterInfo/{chapterId}")
    @ApiOperation(value = "根据id查询")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);
    }

    //3 修改章节
    @PostMapping("updateChapter")
    @ApiOperation(value = "修改章节")
    public R updateChapter(@RequestBody EduChapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    //4 删除章节
    @DeleteMapping("{chapterId}")
    @ApiOperation(value = "删除章节")
    public R deleteChapter(@PathVariable String chapterId) {
        //章节里面可能有小节
        boolean flag = chapterService.deleteChapter(chapterId);
        return R.ok();
    }


}

